package com.msi.shortwave;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

public class freq extends Fragment {
	
public static String getOnlyNumericsfreq(String str) {
	    
	    if (str == null) {
	        return null;
	    }

	    StringBuffer strBuff = new StringBuffer();
	    char c;
	    
	    for (int i = 0; i < str.length() ; i++) {
	        c = str.charAt(i);
	        
	        if (Character.isDigit(c)) {
	            strBuff.append(c);
	        }
	    }
	    return strBuff.toString();
	}
	

	private Button searchbutton;
	private TextView timedisplay;
	static final int TIME_DIALOG_ID = 0;
	private String selhour;
	private String selmin;
	private String selfreq;
	
	   @Override
	   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	      // Inflate the layout for this fragment
	      View v = inflater.inflate(R.layout.freq, container, false);
	      
	      SimpleDateFormat f = new SimpleDateFormat("HH");
	    	f.setTimeZone(TimeZone.getTimeZone("UTC"));
	    	
	        SimpleDateFormat m = new SimpleDateFormat("mm");
	    	m.setTimeZone(TimeZone.getTimeZone("UTC"));
	    	
	    	
	    	TimePicker timeselect = (TimePicker) v.findViewById(R.id.timepickerfreq);
	    	
	    	timeselect.setIs24HourView(true);
	    	timeselect.setCurrentHour(Integer.valueOf(f.format(new Date())));
	    	timeselect.setCurrentMinute(Integer.valueOf(m.format(new Date())));
	    	timeselect.setEnabled(false);
	    	f = new SimpleDateFormat("HHmm");
	     	f.setTimeZone(TimeZone.getTimeZone("UTC"));
	    	searchbutton = (Button) v.findViewById(R.id.bsearchtimefreq);
	    	searchbutton.setOnClickListener(new View.OnClickListener() {
	    	public void onClick(View v) {
	    		
	    		EditText freq = (EditText)getActivity().findViewById(R.id.freqbox);
	        	selfreq = freq.getText().toString();
	        	 
	    		if (getOnlyNumericsfreq(selfreq) != ""){
	    		TimePicker timeselect = (TimePicker) getActivity().findViewById(R.id.timepickerfreq);
	    		

	        	final CheckBox checkbox = (CheckBox) getActivity().findViewById(R.id.freqcheck);

	        	if (checkbox.isChecked()){
	        		String strmin =  Integer.toString(timeselect.getCurrentMinute());
	    			String strhour =  Integer.toString(timeselect.getCurrentHour());
	    			if (strmin.length() == 1){
	    			strmin = "0" + strmin;
	    			}
	    			if (strhour.length() == 1){
	    				strhour = "0" + strhour;
	    				}
	    			
	    		selmin = strmin;
	    		selhour = strhour;		
	        	}

	    //here
	    	 
	    	 
		    	FragmentTransaction ft = getFragmentManager().beginTransaction();

		    	Fragment mFragmentR=new results ();
		    	
		    	
		    	
		    	 Bundle bund1 =  new Bundle();
			bund1.putString("station", "");
			
		    	bund1.putString("time", selhour+selmin);
		    	bund1.putString("target", "");
		    	bund1.putString("language", "999");
		    	bund1.putString("freq", selfreq);
			mFragmentR.setArguments(bund1);
			  if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {     
			        ft.replace(R.id.fragment_content2, mFragmentR, null);
			    }else if((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {     

			        ft.replace(R.id.fragment_content2, mFragmentR, null);
			    }else{
		    	ft.replace(R.id.fragment_content, mFragmentR, null);
			    }
			ft.addToBackStack(null);

			ft.commit();
	    	}}});
	    	
	    	final CheckBox checkbox = (CheckBox) v.findViewById(R.id.freqcheck);
	    	checkbox.setOnClickListener(new View.OnClickListener() {
	    	    public void onClick(View v) {
	    	    	TimePicker timeselect = (TimePicker) getActivity().findViewById(R.id.timepickerfreq);

	    	        // Perform action on clicks, depending on whether it's now checked
	    	        if (((CheckBox) v).isChecked()) {
	    	        	
	    	        	timeselect.setEnabled(true);
	    	            SimpleDateFormat f = new SimpleDateFormat("HHmm");
	    	        	f.setTimeZone(TimeZone.getTimeZone("UTC"));
	    	        	selhour = (f.format(new Date()).substring(0,2));
	    	        	selmin = (f.format(new Date()).substring(2,4));
	    	        } else {
	    	        	timeselect.setEnabled(false);
	    	        	selmin = "";
	    	        	selhour = "";
	    	        }
	    	    }
	    	});
	      
	      return v;
	   }
	   
	   public freq() {

	   }
	        
	}